/**
  * AntDroid Copyright (c) 2008-2012 INRIA, University of Lille 1
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  * 
  * Contact: nicolas.haderer@inria.fr
  *
  * Author: Haderer Nicolas
  */
package com.google.antranking.utils;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;



import android.content.Context;

public class CallAtService {

	public String callServiceConnect(Context mContext,String url, String username, String password)
			throws Exception{
		
		if (url.endsWith("/"))
			url = url.substring(0,url.length()-1);
		
		final StringBuffer urlService = new StringBuffer();
		urlService.append(url);
		urlService.append(AtConstant.SERVICE_CONNECTION);
		
		final List<NameValuePair> args = new ArrayList<NameValuePair>();
		args.add(new BasicNameValuePair("username", username));
		args.add(new BasicNameValuePair("password", password));
		
		
		final HttpRequestUtils http = new HttpRequestUtils();
		final InputStream input = http.sendPostRequest(urlService.toString(),args);
		
		return HttpRequestUtils.convertStreamToString(input);
	}
	
	
	public String callServiceCreateAccount(Context context, 
			String url,
			String fullname,
			String username,
			String password,
			String email) throws Exception{
		
		if (url.endsWith("/"))
			url = url.substring(0,url.length()-1);
		
		final List<NameValuePair> args = new ArrayList<NameValuePair>();
		args.add(new BasicNameValuePair("fullname", fullname));
		args.add(new BasicNameValuePair("username", username));
		args.add(new BasicNameValuePair("password", password));
		args.add(new BasicNameValuePair("email", email));
		
		final StringBuffer urlService = new StringBuffer();
		urlService.append(url);
		urlService.append(AtConstant.SERVICE_CREATE_ACCOUNT);
		
		final HttpRequestUtils http = new HttpRequestUtils();
		final InputStream input = http.sendPostRequest(urlService.toString(),args);
		
		final String result = HttpRequestUtils.convertStreamToString(input);
		
		if (result.contains("error")){
			throw new Exception(result);
		}
		
		return "Welcome "+fullname;
		
	}
	
	
	public String callGetExperimentsService(Context context) throws Exception{
		
		final String host = AtConfiguration.getStringValue(context, AtConstant.PREF_HOST,"");
		final String username = AtConfiguration.getStringValue(context, AtConstant.PREF_USERNAME,"");
		final String password = AtConfiguration.getStringValue(context, AtConstant.PREF_PASSWORD,"");
		
		final StringBuilder urlService = new StringBuilder();
		urlService.append(host);
		urlService.append(AtConstant.SERVICE_GET_XPS);
		
		Log.i("call antdroid service : "+urlService.toString());
		
		final List<NameValuePair> args = new ArrayList<NameValuePair>();
		
		final HttpRequestUtils http = new HttpRequestUtils();
		http.setBasicAuthentication(username, password);
		
		final String result = HttpRequestUtils.convertStreamToString(http.sendPostRequest(urlService.toString(), args));
		
		if (result.contains("error")){
			throw new Exception(result);
		}
		
		return result;
	}
	
	
	public String callSubscribedExperimentService(Context context, String appName) throws Exception{
		
		
		final String host = AtConfiguration.getStringValue(context, AtConstant.PREF_HOST,"");
		final String username = AtConfiguration.getStringValue(context, AtConstant.PREF_USERNAME,"");
		final String password = AtConfiguration.getStringValue(context, AtConstant.PREF_PASSWORD,"");
		
		final StringBuilder urlService = new StringBuilder();
		urlService.append(host);
		urlService.append(AtConstant.SERVICE_GET_SCRIPT);
		
		Log.i("call antdroid service : "+urlService.toString());
		
		final List<NameValuePair> args = new ArrayList<NameValuePair>();
		args.add(new BasicNameValuePair("name", appName));
		
		final HttpRequestUtils http = new HttpRequestUtils();
		http.setBasicAuthentication(username, password);
		
		final String result = HttpRequestUtils.convertStreamToString(http.sendPostRequest(urlService.toString(), args));
		
		if (result.contains("error")){
			throw new Exception(result);
		}
		
		return null;
	}
	
	
	public String callSendTrackService(Context context, String name, String date, String data) 
			throws Exception{
		
		final String host = AtConfiguration.getStringValue(context, AtConstant.PREF_HOST,"");
		final String username = AtConfiguration.getStringValue(context, AtConstant.PREF_USERNAME,"");
		final String password = AtConfiguration.getStringValue(context, AtConstant.PREF_PASSWORD,"");
		
		final StringBuilder urlService = new StringBuilder();
		urlService.append(host);
		urlService.append(AtConstant.SERVICE_SEND_TRK);
		
		final List<NameValuePair> args = new ArrayList<NameValuePair>();
		args.add(new BasicNameValuePair("name", name));
		args.add(new BasicNameValuePair("date", date));
		args.add(new BasicNameValuePair("data", data));
		
		final HttpRequestUtils http = new HttpRequestUtils();
		http.setBasicAuthentication(username, password);
		
		final String r = HttpRequestUtils.convertStreamToString(
				http.sendPostRequest(urlService.toString(), args));
		
		if (r.contains("error")){
			throw new Exception(r);
		}
		
		return r;
	}
	
	
}
